<template>
	<view class="my-publish">
		<view class="tab-list">
			<tab-list :list="tabs" v-model="activeTab" @change="tabChange"></tab-list>
		</view>
		<MescrollUni top="81" :down="downOption" @down="downCallback"
		 :up="upOption" @up="upCallback"  @init="mescrollInit" class="scroll-box">
			<view class="product-box" v-for="item in dataList" :key="item.group_id" @click="goDetail(item)">
				<view class="create-time">
					发起时间：{{item.create_time}}
				</view>
				<product-item :info="item" rightText="分享" @right-click="showSharePopup(item)" class="product-item"></product-item>
			</view>
		</MescrollUni>
		<share-popup :groupId="selGroupId" @init="initShareInfo" :visible.sync="sharePopupVisible"></share-popup>
	</view>
</template>

<script>
	import {mergeObj} from '@/common/common.js'
	import TabList from '@/components/tabs.vue'
	import MescrollUni from "@/components/mescroll-uni/mescroll-uni.vue"
	import ProductItem from "@/components/product/item.vue"
	import SharePopup from "@/components/share-popup.vue"
	// #ifdef MP-WEIXIN
	import ShareMixin from '@/mixins/share-mixin.js'
	// #endif
	export default {
		// #ifdef MP-WEIXIN
		mixins: [ShareMixin],
		// #endif
		components:{
			TabList,
			MescrollUni,
			ProductItem,
			SharePopup
		},
		data() {
			return {
				tabs:[{name:'所有的',value:0},{name:'进行中',value:1},{name:'已完成',value:2},{name:'已取消',value:4}],
				activeTab:0,
				dataList:[],
				downOption: {
					autoShowLoading:true,
					use: true
				},
				upOption: {
					use: true, // 是否启用上拉加载; 默认true
					auto: true,
					onScroll: true,
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				mescroll:null,
				sharePopupVisible: false,
				selGroupId:false
			};
		},
		onLoad(){
			uni.$on('refreshSingle',(data)=>{
				console.log('触发on:',data)
				this.onRefreshSingle(data)
			})
		},
		onShow(){
			uni.setNavigationBarTitle({
				title:'心愿圈'
			})
		},
		methods:{
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			tabChange(){
				this.mescroll.triggerDownScroll()
			},
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			showSharePopup(item){
				this.selGroupId=item.group_id
				this.sharePopupVisible = true
			},
			goDetail(item){
				uni.navigateTo({
					url:`/pages/wish/detail?group_id=${item.group_id}`
				})
			},
			upCallback(mescroll) {
				let page = mescroll.num // 页码, 默认从1开始
				let pageSize = mescroll.size // 页长, 默认每页10条
				let state = this.tabs[this.activeTab].value
				this.$api.getPublishList({state,page,page_size:pageSize},{loading:false}).then(
					res=>{
						if(mescroll.num == 1) this.dataList = []; //如果是第一页需手动置空列表
						this.dataList = this.dataList.concat(res)
						setTimeout(()=>{
							mescroll.endSuccess(res.length)
						},300)
					},
					()=>{
						mescroll.endErr()
					}
				)
			},
			onRefreshSingle({wish}){
				console.log(wish)
				if(wish){
					let index=this.dataList.findIndex(item=>item.group_id==wish.group_id)
					console.log(index)
					if(index!==-1){
						console.log(mergeObj(this.dataList[index],wish))
						this.dataList.splice(index,1,mergeObj(this.dataList[index],wish))
					}
				}
			}
		}
	}
</script>

<style lang="scss">
.my-publish{
	
}
.product-box{
	padding: 0 36rpx;
	.item-good-box{
		margin-top: 4rpx;
	}
}
.create-time{
	margin-top: 40rpx;
	font-size: 22rpx;
	color:#999;
}
</style>
